package com.base.controller;

import com.base.po.tbk.TbkChoiceActivity;
import com.base.service.TbkChoiceActivityService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 精选专场
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class index_ChoiceActivity {

    @Autowired
    private TbkChoiceActivityService tbkChoiceActivityService; // 分类信息

    // 分类广告首页
    @RequestMapping("/choiceActivityIndex")
    public ModelAndView choiceActivityIndex(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录

        Page page = new Page();
        List<TbkChoiceActivity> tbkChoiceActivity = null;

        page.setCurrentPageNo(1);
        page.setRecordTotal(tbkChoiceActivityService.countAll(userid));
        tbkChoiceActivity = tbkChoiceActivityService.selectAll(page, userid);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkChoiceActivity", tbkChoiceActivity);
        modelAndView.setViewName("jsp/index_ChoiceActivity");
        return modelAndView;
    }

    @RequestMapping("/selectChoiceActivity")
    public ModelAndView selectChoiceActivity(HttpServletRequest request, Integer curPage) throws Exception {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }

        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        Page page = new Page();
        List<TbkChoiceActivity> tbkChoiceActivity = null;

        page.setCurrentPageNo(curPage);
        page.setRecordTotal(tbkChoiceActivityService.countAll(adminUserId));
        tbkChoiceActivity = tbkChoiceActivityService.selectAll(page, adminUserId);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkChoiceActivity", tbkChoiceActivity);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_ChoiceActivity");
        return modelAndView;
    }

    // 跳转添加分类
    @RequestMapping("/addChoiceActivity")
    public String addCompanyCarousel() {
        return "jsp/add_ChoiceActivity";
    }

    // 跳转修改分类
    @RequestMapping("/updateChoiceActivity")
    public ModelAndView updateCompanyCarousel(String id) {
        return new ModelAndView("jsp/update_ChoiceActivity", "sActivity", tbkChoiceActivityService.selectByPrimaryKey(id));
    }
}
